program BlackSpace2;

uses
  SysUtils,
  Graphics,
  Dialogs,
  classes,
  ExtCtrls,
  Math,
  Gen2 in 'C:\Program Files (x86)\Embarcadero\Gen2_v1.4\Gen2.pas',
  G2Math in 'C:\Program Files (x86)\Embarcadero\Gen2_v1.4\G2Math.pas',
  G2Avi in 'C:\Program Files (x86)\Embarcadero\Gen2_v1.4\G2Avi.pas',
  G2MeshLoader in 'C:\Program Files (x86)\Embarcadero\Gen2_v1.4\G2MeshLoader.pas',
  DXTypes in 'C:\Program Files (x86)\Embarcadero\DirectX9\DXTypes.pas',
  Direct3D9 in 'C:\Program Files (x86)\Embarcadero\DirectX9\Direct3D9.pas',
  D3DX9 in 'C:\Program Files (x86)\Embarcadero\DirectX9\D3DX9.pas',
  DirectInput in 'C:\Program Files (x86)\Embarcadero\DirectX9\DirectInput.pas',
  cuVector in '..\..\_common\cuVector.pas',
  cuDebug in '..\..\_common\cuDebug.pas',
  cuSplitFile in '..\..\_common\cuSplitFile.pas',
  cuSplitLine in '..\..\_common\cuSplitLine.pas',
  cuFuncs in '..\..\_common\cuFuncs.pas',
  cuMath64 in '..\..\_common\cuMath64.pas',
  cuSimpleINI in '..\..\_common\cuSimpleINI.pas',
  cuSimpleArrays in '..\..\_common\cuSimpleArrays.pas',
  uBaseObject in 'uBaseObject.pas',
  uDefs in 'uDefs.pas',
  uUniverse in 'uUniverse.pas',
  uSolarSystem in 'uSolarSystem.pas',
  uPlanet in 'uPlanet.pas',
  uSettings in 'uSettings.pas',
  uEmpire in 'uEmpire.pas',
  uGame in 'uGame.pas',
  cuZoom in '..\..\_common\cuZoom.pas';

type
  TMyApp = class (TG2App)
  public
    MgrTextures : TG2TextureMgr;
    MgrFonts    : TG2FontMgr;
    Font1       : TG2Font;
    MainGUI     : TG2Texture2D;

    StarMap     : TG2Texture2DRT;
    MiniMap     : TG2Texture2DRT;
    ColonyMap   : TG2Texture2DRT;
    FleetMap    : TG2Texture2DRT;

    Game        : TGame;

    procedure OnRender; override;
    procedure OnUpdate; override;
    procedure OnMouseDown(const Button: Byte); override;
    procedure OnKeyDown(const Key: Byte); override;

    procedure createStarMapTexture();
    procedure drawSolarSystems(theWidth,theHeight:integer);

    procedure Initialize; override;
    procedure Finalize; override;
  end;

var
  App: TMyApp;

//TMyApp BEGIN
procedure TMyApp.OnRender;
begin
  Render.Clear(False, False, True, $00000000);
  Render.RenderStart;

  Font1.Print(10,10, $FFFFFFFF, 'FPS: ' + IntToStr(Tmr.FPS) );

  Render.RenderStop;
  Render.Present;
end;

procedure TMyApp.OnUpdate;
begin

end;

procedure TMyApp.OnMouseDown(const Button: Byte);
begin
end;

procedure TMyApp.OnKeyDown(const Key: Byte);
begin
  case Key of
    DIK_ESCAPE     : Running := False;
    DIK_RETURN     : ;
    DIK_SPACE      : ;
    DIK_EQUALS     : ;
    DIK_MINUS      : ;
    DIK_BACK       : ;
    DIK_F1         : ;
    DIK_F2         : ;
    DIK_F3         : ;
    DIK_F4         : ;
    DIK_F5         : ;
    DIK_F6         : ;
    DIK_F7         : ;
    DIK_F8         : ;
    DIK_RIGHTARROW : ;
    DIK_LEFTARROW  : ;
    DIK_DOWNARROW  : ;
    DIK_UPARROW    : ;
    DIK_TAB        : ;
    DIK_Q          : ;
    DIK_Z          : ;
  end;
end;

//Drawing textures
procedure TMyApp.createStarMapTexture();
begin
  DevConsole.Print(LOG_DEBUG2, 'TMyApp.createStarMapTexture');

  Gfx.SetRenderTargetTexture2D( StarMap );

  Render.RenderStart;

  Render2D.DrawRect(0, 0, TG2Texture2D(MgrTextures.FindTexture('bg')) );

//  drawStarLanes();

  drawSolarSystems(0,0);

  Render.RenderStop;

  Gfx.SetRenderTargetDefault();//Goes back to back buffer

//  DevConsole.Print(ES_DEBUG, '[TMyApp] end - CreateStarMapTexture()');
end;

procedure TMyApp.drawSolarSystems(theWidth,theHeight:integer);
var S, nX, nY, OffSet  : integer;
begin
  Offset := 16;

  with Game.Universe do
  for S := 0 to SolarSystems.Count-1 do
  begin
    with SolarSystems.Items[ S ] do
    begin
      Vector2D.fitToScreen(Game.focusVector.X, Game.focusVector.Y, SettingsINI.iBlackSpace, Game.theZoom.Value);

      nX := Vector2D.altX;
      nY := Vector2D.altY;

      case solarStarType of
        climateBLUESTAR   : Render2D.DrawRect(nX-Offset, nY-Offset, TG2Texture2D(MgrTextures.FindTexture('bluestar')));
        climateYELLOWSTAR : Render2D.DrawRect(nX-Offset, nY-Offset, TG2Texture2D(MgrTextures.FindTexture('yellowstar')));
        climateREDSTAR    : Render2D.DrawRect(nX-Offset, nY-Offset, TG2Texture2D(MgrTextures.FindTexture('redstar')));
      end;
    end;
  end;

  Game.updateStarMap( FALSE );
end;


procedure TMyApp.Initialize;
begin
  Randomize;

  DevConsole := TDebugConsole.Create(GameName+' / '+Copyright+' / '+Version);
  DevConsole.setLogLevel('1 2 3 4 5 6 7 8');

  DevConsole.Print(LOG_DEBUG1, 'RandTest:'+RandTest(300) );

  DevConsole.Print(LOG_DEBUG1, 'RandTest:'+RandTest(200) );

  SettingsINI := TSettingsINI.Create();
  SettingsINI.loadSettings('settings.ini');

  DevConsole.setLogLevel( SettingsINI.sFind('sDebug') );

  Gfx.InitParams.Width := SettingsINI.iFind('iScreenWidth');
  Gfx.InitParams.Height := SettingsINI.iFind('iScreenHeight');
  Gfx.InitParams.FullScreen := SettingsINI.bFind('bFullscreen');

  Gfx.InitParams.FormatBackBuffer := D3DFMT_A8R8G8B8;
  Gfx.InitParams.FormatSurfaceRT := D3DFMT_A8R8G8B8;
  Gfx.InitParams.FormatSurfaceDS := D3DFMT_D16;
  g2.Handle := App.WindowCreate(Gfx.InitParams.Width, Gfx.InitParams.Height, 'Blackspace - Alpha');
  Tmr.Mode := tmManual;
  Tmr.MaxFPS := 0;
  inherited Initialize;
  g2.RequestMod(TG2TextureMgr, @MgrTextures);
  g2.RequestMod(TG2FontMgr, @MgrFonts);
  Font1 := Gfx.Shared.RequestFont('Times New Roman', 12);

  MgrTextures.CreateTexture2DFromFile('ship'       , 'images\ships\ship.png'              );
  MgrTextures.CreateTexture2DFromFile('smallfleet' , 'images\ships\f_smallfleet.png'      );
  MgrTextures.CreateTexture2DFromFile('mediumfleet', 'images\ships\f_mediumfleet.png'     );
  MgrTextures.CreateTexture2DFromFile('largefleet' , 'images\ships\f_largefleet.png'      );
  MgrTextures.CreateTexture2DFromFile('colonized'  , 'images\gui\colonized.png'           );
  MgrTextures.CreateTexture2DFromFile('bg'         , 'images\galaxy\bg.png'               );

  MgrTextures.CreateTexture2DFromFile('desert'   , 'images\astros\p_smalldesert.png'      );
  MgrTextures.CreateTexture2DFromFile('gas'      , 'images\astros\p_smallgas.png'         );
  MgrTextures.CreateTexture2DFromFile('gaia'     , 'images\astros\p_smallgaia.png'        );
  MgrTextures.CreateTexture2DFromFile('ocean'    , 'images\astros\p_smallocean.png'       );
  MgrTextures.CreateTexture2DFromFile('ice'      , 'images\astros\p_smallice.png'         );

  MgrTextures.CreateTexture2DFromFile('bluestar'   , 'images\astros\s_smallbluestar.png'  );
  MgrTextures.CreateTexture2DFromFile('redstar'    , 'images\astros\s_smallredstar.png'   );
  MgrTextures.CreateTexture2DFromFile('yellowstar' , 'images\astros\s_smallyellowstar.png');
  MgrTextures.CreateTexture2DFromFile('orangestar' , 'images\astros\s_smallorangestar.png');

  Game := TGame.Create();
  Game.Universe.createUniverse();

  Tmr.Enabled := True;
end;
procedure TMyApp.Finalize;
begin
  inherited Finalize;
end;
//TMyApp END

begin
  App := TMyApp.Create;
  App.Initialize;
  if App.g2.Initialized then
  try
    App.Loop;
  except
    on E : Exception do
    begin
      App.Free;
    end;
  end;
  App.Free;
  ExitCode := 0;
end.
